"use strict";
$(window).on("load", function () {
    document.title = '苏州电信智慧物联网管理平台';
    setTimeout(function () {
        $(".page-loader").fadeOut();
    }, 300);
}), $(document).ready(function () {
    if (localStorage.getItem("data-ma-theme")) {
        var themeLoc = localStorage.getItem("data-ma-theme");
        $("body").attr("data-ma-theme", themeLoc);
        var themeClass = ".bg-" + themeLoc;
        var $theme_switched = $(".theme-switch .btn" + themeClass),
            $theme_not = $theme_switched.siblings(".btn");
        $theme_switched.addClass("active");
        $theme_not.removeClass("active");
        $theme_switched.find("input:radio").attr("checked");
        $theme_not.find("input:radio").removeAttr("checked");
    }
    $("body").on("change", ".theme-switch input:radio", function () {
        var a = $(this).val();
        $("body").attr("data-ma-theme", a);
        localStorage.setItem("data-ma-theme", a);
    }), $("body").on("focus", ".search__text", function () {
        $(this).closest(".search").addClass("search--focus")
    }), $("body").on("blur", ".search__text", function () {
        $(this).val(""), $(this).closest(".search").removeClass("search--focus")
    }), $("body").on("click", ".navigation__sub > a", function (a) {
        a.preventDefault(), $(this).parent().toggleClass("navigation__sub--toggled"), $(this).next("ul").slideToggle(250)
    }), $(".form-group--float")[0] && ($(".form-group--float").each(function () {
        0 == !$(this).find(".form-control").val().length && $(this).find(".form-control").addClass("form-control--active")
    }), $("body").on("blur", ".form-group--float .form-control", function () {
        0 == $(this).val().length ? $(this).removeClass("form-control--active") : $(this).addClass("form-control--active")
    }))
}), $(document).ready(function () {
    function a(a) {
        a.requestFullscreen ? a.requestFullscreen() : a.mozRequestFullScreen ? a.mozRequestFullScreen() : a.webkitRequestFullscreen ? a.webkitRequestFullscreen() : a.msRequestFullscreen && a.msRequestFullscreen()
    }


    $(window).resize(function (ev) {
        var notFullScreen = (document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen);
        console.log(notFullScreen);
    });

    if ($("select.select2")[0]) {
        var a = $(".select2-parent")[0] ? $(".select2-parent") : $("body");
        $("select.select2").select2({dropdownAutoWidth: !0, width: "100%", dropdownParent: a})
    }
    $("body").on("click", "[data-ma-action]", function (b) {
        b.preventDefault();
        var c = $(this), d = c.data("ma-action"), e = "";
        switch (d) {
            case"search-open":
                $(".search").addClass("search--toggled");
                break;
            case"search-close":
                $(".search").removeClass("search--toggled");
                break;
            case"aside-open":
                e = c.data("ma-target"), c.addClass("toggled"), $(e).addClass("toggled"), $(".content, .header").append('<div class="ma-backdrop" data-ma-action="aside-close" data-ma-target=' + e + " />");
                break;
            case"aside-close":
                e = c.data("ma-target"), $('[data-ma-action="aside-open"], ' + e).removeClass("toggled"), $(".content, .header").find(".ma-backdrop").remove();
                break;
            case"fullscreen":
                // a(document.documentElement);
                var elem = document.documentElement;
                if ((document.fullScreenElement !== undefined && document.fullScreenElement === null) || (document.msFullscreenElement !== undefined && document.msFullscreenElement === null) || (document.mozFullScreen !== undefined && !document.mozFullScreen) || (document.webkitIsFullScreen !== undefined && !document.webkitIsFullScreen)) {
                    // $(this).html('退出全屏');
                    if (elem.requestFullScreen) {
                        elem.requestFullScreen();
                    } else if (elem.mozRequestFullScreen) {
                        elem.mozRequestFullScreen();
                    } else if (elem.webkitRequestFullScreen) {
                        elem.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
                    } else if (elem.msRequestFullscreen) {
                        elem.msRequestFullscreen();
                    }
                } else {
                    // $(this).html('全屏');
                    if (document.cancelFullScreen) {
                        document.cancelFullScreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                }
                break;
            case"print":
                window.print();
                break;
            case"clear-localstorage":
                localStorage.clear();
                break;
            case"login-switch":
                e = c.data("ma-target"), $(".login__block").removeClass("active"), $(e).addClass("active");
                break;
            case"notifications-clear":
                b.stopPropagation();
                var f = $(".top-nav__notifications .listview__item"), g = f.length, h = 0;
                c.fadeOut(), f.each(function () {
                    var a = $(this);
                    setTimeout(function () {
                        a.addClass("animated fadeOutRight")
                    }, h += 150)
                }), setTimeout(function () {
                    f.remove(), $(".top-nav__notifications").addClass("top-nav__notifications--cleared")
                }, 180 * g);
                break;
            case"toolbar-search-open":
                $(this).closest(".toolbar").find(".toolbar__search").fadeIn(200), $(this).closest(".toolbar").find(".toolbar__search input").focus();
                break;
            case"toolbar-search-close":
                $(this).closest(".toolbar").find(".toolbar__search input").val(""), $(this).closest(".toolbar").find(".toolbar__search").fadeOut(200)
        }
    })
}), $(document).ready(function () {
    $("[data-layer-url]").css('cursor', 'pointer');
    $(document).on("click", "[data-layer-url]", function () {
        var $this = $(this);
        var url = $this.data("layer-url");
        if (!url) return false;
        var layt = $this.data("layer-title");
        var layw = $this.data("layer-width");
        var layh = $this.data("layer-height");
        var title = layt ? layt : "";
        var width = layw ? layw : "600px";
        var height = layh ? layh : "85%";
        layer.open({
            type: 2, //层的类型。0：信息框（默认），1：页面层，2：iframe层，3：加载层，4：tips层。
            title: [title, 'background:#2D3F51;color:#FFFFFF;'], //控制默认标题栏。title:'标题'，自定义标题样式，可以title: ['标题', 'background:#c00;'] 第二个参数可书写任意css
            //offset: ['', ''], //[纵坐标, 横坐标]，默认为垂直水平居中
            area: [width, height], //[宽度, 高度]
            //maxmin: true, //是否输出窗口最小化/全屏/还原按钮,默认false
            shade: [0.3, '#000'], //控制遮罩[0.5, '#000']（[透明度0~1, '色值']）,设置为false则不显示
            shadeClose: true, //遮罩显示情况下，控制点击遮罩区域是否关闭层
            anim: false, //动画类型0~6 默认0 设置false为不使用动画
            scrollbar: false, //屏蔽浏览器滚动条
            content: url //页面地址
        });
    });
});

function dataTables_init() { //初始化表格
    $(document).ready(function () {
        if ($("#data-table")[0]) {
            $("#data-table").DataTable({
                searching: false, //是否搜索
                ordering: false, //是否排序
                // autoWidth: false,
                // responsive: false,
                bPaginate: true,
                language: {
                    processing: '<i class="zmdi zmdi-spinner zmdi-hc-2x zmdi-hc-spin"></i>',
                    searchPlaceholder: "搜索...",
                    sZeroRecords: "没有匹配项...",
                    sInfo: "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                    sInfoEmpty: "显示第 0 至 0 项结果，共 0 项",
                    sInfoFiltered: "（总共 _MAX_ 项）",
                    sEmptyTable: "数据为空",
                    sLoadingRecords: "载入中...",
                    oPaginate: {
                        sFirst: "首页",
                        // sPrevious: "上页",
                        // sNext: "下页",
                        sLast: "末页"
                    }
                },
                pagingType: 'full_numbers',
                paging: true, //是否分页
                lengthMenu: [[10, 25, 50, 100], ["显示10项", "显示25项", "显示50项", "显示100项"]],
                pageLength: 10,//默认每页显示多少条
                sDom: '<"dataTables__top"lfB>rt<"dataTables__bottom"ip><"clear">', //功能按钮
                buttons: [
                    {
                        extend: "excelHtml5",
                        title: "Export Data"
                    },
                    {
                        extend: "csvHtml5",
                        title: "Export Data"
                    }, {
                        extend: "print",
                        title: "Print Data"
                    }
                ],
                fnInitComplete: function (oSettings, json) {
                    $(".dataTables_filter input[type=search]").focus(function () {
                        $(this).closest(".dataTables_filter").addClass("dataTables_filter--toggled");
                    }).blur(function () {
                        $(this).closest(".dataTables_filter").removeClass("dataTables_filter--toggled");
                    });

                    var $dTable = $('#data-table'), $dTable_iScroll, $dTable_s = $(".table-responsive");
                    $dTable.wrap("<div id='table-iScroll'></div>");
                    $dTable_iScroll = $('#table-iScroll');
                    $dTable_iScroll.scrollLeft(1);
                    $dTable_iScroll.scroll(function () {
                        var $this = $(this);
                        if ($this.scrollLeft() == 0) {
                            $dTable.removeClass('rightFixed');
                        } else {
                            $dTable.addClass('rightFixed');
                        }
                    });


                }
            }), $(".dataTables_filter input[type=search]").focus(function () {
                $(this).closest(".dataTables_filter").addClass("dataTables_filter--toggled")
            }), $(".dataTables_filter input[type=search]").blur(function () {
                $(this).closest(".dataTables_filter").removeClass("dataTables_filter--toggled")
            }), $("body").on("click", "[data-table-action]", function (a) {
                a.preventDefault();
                var b = $(this).data("table-action");
                if ("excel" === b && $(this).closest(".dataTables_wrapper").find(".buttons-excel").trigger("click"), "csv" === b && $(this).closest(".dataTables_wrapper").find(".buttons-csv").trigger("click"), "print" === b && $(this).closest(".dataTables_wrapper").find(".buttons-print").trigger("click"), "fullscreen" === b) {
                    var c = $(this).closest(".card");
                    c.hasClass("card--fullscreen") ? (c.removeClass("card--fullscreen"), $("body").removeClass("data-table-toggled")) : (c.addClass("card--fullscreen"), $("body").addClass("data-table-toggled"))
                }
            })
        }
        $(".scrollbar-inner")[0] && $(".scrollbar-inner").scrollbar().scrollLock();
    })
}

dataTables_init();

/**
 * 危险操作前的确认
 * @param str @string 操作名称
 * @param func @function 操作
 */
function dangerAction(str, func) {
    var msg = "是否" + str + "？";
    layer.confirm(msg, {title: [str, 'background:#2D3F51;color:#FFFFFF;'], shadeClose: true}, function (index) {
        func();
        layer.close(index);
    });
}

$(function () {
    $(document).on("click", "tr .btn.btn-danger.btn-sm:contains('删除')", function () { //模拟删除
        var $this = $(this), $thisTr = $this.parents("tr");
        dangerAction("删除", function () {
            $thisTr.fadeOut("slow", function () {
                $(this).remove();
            });
        });
    });

    $(document).on("click", "tr .btn.alarmOk", function () {
        var $this = $(this), $thisTr = $this.parents("tr");
        dangerAction("确认报警", function () {
            $this.attr('disabled', 'disabled');
        });
    });

    $(document).on("click", "tr .btn.alarmCancel", function () {
        var $this = $(this), $thisTr = $this.parents("tr");
        layer.prompt({title: ['解除说明', 'background:#2D3F51;color:#FFFFFF;'], formType: 2}, function (text, index) {
            layer.close(index);

            layer.msg('已解除报警：' + text);
            $thisTr.fadeOut("slow", function () {
                $(this).remove();
            });
        });
    });
});
